$(function() {
    var arrProductCode = Array();

	$('#XT-Add').bind('click',function(){
        var arrChk = Array();
        var arrTxt = Array();

        $('input[name="item"]:checked').each(function(){ 
            if($.inArray($(this).val(), arrChk)<0)
            {
                arrChk.push($(this).val());
                arrTxt.push($(this).attr('_text'));
            }
        }); 

        var msg = '';
        if(arrChk.length==0)
            msg = '请选择工作内容！';
        else if( $("#workscene_flag").val() == 1 && $('input:radio[name="scene"]:checked').length == 0)
            msg = '请选择工作场景！';
        else if( $('input:radio[name="time"]:checked').length == 0)
            msg = '请选择计价方式！';
        var userType = $("#user_type_flag").val();
        if(userType ==1)
        {
        	if( $('input:radio[name="shoottype"]:checked').length == 0)
        		msg = "请选择工作形式";
        }
        //验证款式拍摄
        if($('input:radio[name="shoottype"]:checked').val()==1 && msg=='')
        {
        	if($.trim($("#shootpic_ordinary").val())==undefined || $.trim($("#shootpic_ordinary").val())=="")
        	{
        		msg = "请输入款式拍摄件数";
        	}
        	else if(isNaN($.trim($("#shootpic_ordinary").val())))
        	{
        		msg = "款式拍摄件数输入有误";
        	}
        }
        //验证形象拍摄
        if($('input:radio[name="shoottype"]:checked').val()==2 && msg=='')
        {
        	if($.trim($("#shootpic_fine").val())==undefined || $.trim($("#shootpic_fine").val())=="")
        	{
        		msg = "请输入形象拍摄件数";
        	}
        	else if(isNaN($.trim($("#shootpic_fine").val())))
        	{
        		msg = "形象拍摄件数输入有误";
        	}
        }
        //验证内衣泳装
        if($('input:radio[name="swimsuit"]:checked').val() == 1 && msg == '')
        {
        	if($.trim($("#swimsuit_percentage").val())==undefined || $.trim($("#swimsuit_percentage").val())=="")
        	{
        		msg = "请输入内衣泳装加收";
        	}
        	else if(isNaN($.trim($("#swimsuit_percentage").val())))
        	{
        		msg = "内衣泳装加收输入有误";
        	}
        }
        //验证情趣内衣
        if($('input:radio[name="appealsuit"]:checked').val() == 1 && msg == '')
        {
        	if($.trim($("#appealsuit_percentage").val())==undefined || $.trim($("#appealsuit_percentage").val())=="")
        	{
        		msg = "请输入情趣内衣加收";
        	}
        	else if(isNaN($.trim($("#appealsuit_percentage").val())))
        	{
        		msg = "情趣内衣加收输入有误";
        	}
        }
        $('#alertmsg').html(msg);
        $("#alertmsg").show();
        if(msg!='')
            return false;
        
        var htmlItem = $('#priceitem').html();//$('#priceList').find("li:first-child").clone().html();
        var scene_text  = $("#workscene_flag").val()==1?$('input:radio[name="scene"]:checked').attr('_text'):'';
        var time_text  = $('input:radio[name="time"]:checked').attr('_text');
        var scene  = $("#workscene_flag").val()==1?$('input:radio[name="scene"]:checked').val():0;
        var time  = $('input:radio[name="time"]:checked').val();
        //款式拍摄/形象拍摄
        var shoottype = $('input:radio[name="shoottype"]:checked').val();
        var shootpic = 0;
        var shoot_text = '';
        if(shoottype == undefined || shoottype == '')
        {
        	shoottype = 0;
        }
        if(shoottype==1)
        {
        	shootpic = $("#shootpic_ordinary").val();
        	if(shootpic == 0)
        		shootpic = '不限';
        	shoot_text = '款式拍摄'+shootpic+'件起拍';
        }
        else if(shoottype==2)
        {
        	shootpic = $("#shootpic_fine").val();
        	if(shootpic == 0)
        		shootpic = '不限';
        	shoot_text = '形象拍摄'+shootpic+'件起拍';
        }
        else if(shoottype == 3)
        {
        	shoot_text = '不限';
        }
        //内衣泳装
        var swimsuit = $('input:radio[name="swimsuit"]:checked').val();
        var swimsuit_text = '';
        if(swimsuit == undefined || swimsuit == '')
        {
        	swimsuit = 0;
        }
    	if(swimsuit == 1)
    	{
    		swimsuit_text = "内衣泳装加收"+$("#swimsuit_percentage").val()+'%';
    		swimsuit = $("#swimsuit_percentage").val();
    	}
    	if(swimsuit == 0.1)
    	{
    		swimsuit_text = '不接内衣泳装';
    	}
        //情趣内衣
        var appealsuit = $('input:radio[name="appealsuit"]:checked').val();
        var appealsuit_text = "";
        if(appealsuit == undefined || appealsuit == '')
        {
        	appealsuit = 0;
        }
    	if(appealsuit == 1)
    	{
    		appealsuit_text = '情趣内衣加收'+$("#appealsuit_percentage").val()+'%';
    		appealsuit = $("#appealsuit_percentage").val();
    	}
    	else if(appealsuit == 0.1)
    	{
    		appealsuit_text = '不接情趣内衣';
    	}
        
        var htmlTmp = '';
        $.each(arrChk, function(idx,item){
            var code = item+'_'+scene+'_'+time;
            if(userType == 1)
            	code += '_'+shoottype;
            if($.inArray(code, arrProductCode)<0)
            {
                arrProductCode.push(code);
                $htmlStr = arrTxt[idx];
                if(scene_text != '')
                {
                	$htmlStr += ' + '+scene_text;
                }
                $htmlStr += ' + '+time_text;
                if(shoot_text != '')
                {
                	$htmlStr += ' + '+shoot_text;
                }
                if(swimsuit_text != '')
                {
                	$htmlStr += ' + '+swimsuit_text;
                }
                if(appealsuit_text !='')
                {
                	$htmlStr += ' + '+appealsuit_text;
                }
                htmlTmp += htmlItem.replace(/{{_eg_}}/g,'').replace(/{{item_code}}/g, code).replace(/{{shoottype}}/g, shoottype).replace(/{{shootpic}}/g, shootpic).replace(/{{swimsuit}}/g, swimsuit).replace(/{{appealsuit}}/g, appealsuit).replace(/{{item_work}}/g, $htmlStr);
            }
        });
        
        if(htmlTmp!='')
            $('#priceList').append(htmlTmp);
        if($('#priceList').html()=='')
            $('#XT-Submit').css('display','none');
        else
    	{
    		$("#work_remark").show();
    		$('#XT-Submit').addClass('but');
    	}

        //点击添加之后清空选项
        $('#XT-Reset').click();
    });

    //点击工作场景清空其下的选项
	$('input:radio[name="scene"]').click(function(){
		if($('input:radio[name="time"]:checked').length>0)
            $('input:radio[name="time"]:checked').attr('checked',false);
        //清空普拍、精拍
        if($('input:radio[name="shoottype"]:checked').length>0)
        	$('input:radio[name="shoottype"]:checked').attr('checked',false);
        $("#shootpic_ordinary").val('');
        $("#shootpic_fine").val('');
        //清空内衣泳装
        if($('input:radio[name="swimsuit"]:checked').length>0)
        	$('input:radio[name="swimsuit"]:checked').attr('checked',false);
        $("#swimsuit_percentage").val('');
        //清空情趣内衣
        if($('input:radio[name="appealsuit"]:checked').length>0)
        	$('input:radio[name="appealsuit"]:checked').attr('checked',false);
        $("#appealsuit_percentage").val('');
	});
	
	//点击计价方式清空其下的选项
	$('input:radio[name="time"]').click(function(){
		//清空普拍、精拍
        if($('input:radio[name="shoottype"]:checked').length>0)
        	$('input:radio[name="shoottype"]:checked').attr('checked',false);
        $("#shootpic_ordinary").val('');
        $("#shootpic_fine").val('');
        //清空内衣泳装
        if($('input:radio[name="swimsuit"]:checked').length>0)
        	$('input:radio[name="swimsuit"]:checked').attr('checked',false);
        $("#swimsuit_percentage").val('');
        //清空情趣内衣
        if($('input:radio[name="appealsuit"]:checked').length>0)
        	$('input:radio[name="appealsuit"]:checked').attr('checked',false);
        $("#appealsuit_percentage").val('');
	});
	
	//点击工作形式清空其下选项
	$('input:radio[name="shoottype"]').click(function(){
		$("#shootpic_ordinary").val('');
        $("#shootpic_fine").val('');
        //清空内衣泳装
        if($('input:radio[name="swimsuit"]:checked').length>0)
        	$('input:radio[name="swimsuit"]:checked').attr('checked',false);
        $("#swimsuit_percentage").val('');
        //清空情趣内衣
        if($('input:radio[name="appealsuit"]:checked').length>0)
        	$('input:radio[name="appealsuit"]:checked').attr('checked',false);
        $("#appealsuit_percentage").val('');
	});
	
	
    $('#XT-Reset').bind('click',function(){
        $('input[name="item"]:checked').each(function(){ 
            $(this).attr('checked',false);
        });

        if($('input:radio[name="scene"]:checked').length>0)
            $('input:radio[name="scene"]:checked').attr('checked',false);
        if($('input:radio[name="time"]:checked').length>0)
            $('input:radio[name="time"]:checked').attr('checked',false);
        //清空普拍、精拍
        if($('input:radio[name="shoottype"]:checked').length>0)
        	$('input:radio[name="shoottype"]:checked').attr('checked',false);
        $("#shootpic_ordinary").val('');
        $("#shootpic_fine").val('');
        //清空内衣泳装
        if($('input:radio[name="swimsuit"]:checked').length>0)
        	$('input:radio[name="swimsuit"]:checked').attr('checked',false);
        $("#swimsuit_percentage").val('');
        //清空情趣内衣
        if($('input:radio[name="appealsuit"]:checked').length>0)
        	$('input:radio[name="appealsuit"]:checked').attr('checked',false);
        $("#appealsuit_percentage").val('');
    });
});
function delPriceItem(id) {
$.ajax( {
    url:'/m/product/delProduct',
    data:{
            id : id
        },
    type:'post',
    cache:false,
    dataType:'json',
    success:function(res) {
        if(res.code == 200 ){
                window.location.reload(true);
            } else{
                alert('删除失败！！');
	    }
        }
    });
};
